<template>
	<view style="padding: 1rem;">
		<view class="item" @click="workLinesSelect">
			<view class="t-title">选择线体<i>*</i></view>
			<view class="content-select">
				<view class="v-select">{{fromData.factoryName?fromData.factoryName:"请选择"}}</view>
				<view class="iconfont icon-jiantou" style="margin-top: 8rpx;"></view>
			</view>
		</view>
		<view class="item" @click="workProcedureSelect">
			<view class="t-title">班组<i>*</i></view>
			<view class="content-select">
				<view class="v-select">{{fromData.workProcedureName?fromData.workProcedureName:"请选择"}}</view>
				<view class="iconfont icon-jiantou" style="margin-top: 8rpx;"></view>
			</view>
		</view>
		<view class="item" @click="improTypeSelect">
			<view class="t-title">选择改善类别<i>*</i></view>
			<view class="content-select">
				<view class="v-select">{{fromData.improType?fromData.improType:'请选择'}}</view>
				<view class="iconfont icon-jiantou" style="margin-top: 8rpx;"></view>
			</view>
		</view>
		<view class="item">
			<view class="t-title">改善标题<i>*</i></view>
			<input class="item-content-select" style="height: 2rem;" placeholder="请输入标题"
				v-model="fromData.improTitle" />
		</view>
		<view>
			<view class="t-title" style="margin:1rem 0px">改善内容<i>*</i></view>
			<textarea style="border: 0.4px solid #eee;width: 100%;padding: 0.8rem;" placeholder="请输入改善内容(详细)"
				v-model="fromData.cartonInfo">
			</textarea>
		</view>
		<view class="item">
			<view class="t-title">本次是否利于减碳<i></i></view>
			<switch style="transform: scale(0.7);" :checked="check" @change="switchChange"></switch>
		</view>
		<view style="border: #333 1px dashed;padding: 0.5rem;margin: 0.5rem;border-radius: 1rem;" v-if="check">
			<view class="item" @click="cerTypeSelect">
				<view class="t-title">选择减碳种类（多选）<i>*</i></view>
				<view class="content-select">
					<view class="v-select">选择</view>
					<view class="iconfont icon-jiantou" style="margin-top: 8rpx;"></view>
				</view>
			</view>
			<view class="item" v-for="(item,index) in fromData.list " style="background-color: lightgreen;">
				<view style="flex: 1;">减少{{item.reduceType}}使用</view>
				<input class="item-content-select" style="height: 2rem;width: 40%;background-color: #eee;"
					placeholder="单月减少量" v-model="item.reduceCount" />
				 <text class="unit">{{item.reduceUnit}}</text>
				<uni-icons type="clear" size="30" @click="deletType(index)"></uni-icons>
			</view>
		</view>
		<view>
			<view class="t-title" style="margin:1rem 0px">其他改善建议</view>
			<textarea style="border: 0.4px solid #eee;width: 100%;padding: 0.8rem;" placeholder="请输入其他改善建议"
				v-model="fromData.otherAdvice">
			</textarea>
		</view>
		<view class="item-title" style="margin-top: 4rem;">
			<button type="primary" @click="doAdd">提交</button>
		</view>
	</view>
</template>

<script>
	import processImprovementApi from "@/pages_cer/api/process-improvement/process-improvement.js"
	import commonConstants from "@/common/constants/commonConstants.js"
	import dataCom from "@/pages_cer/components/data_comment.js"
	let that;
	export default {
		data() {
			return {
				fromData: {
					factoryCode: '', //线体编码
					factoryName: '',
					workProcedureCode: '', //班组编码
					workProcedureName: '',
					improType: '', //改善类别
					improTitle: '', //工艺改善标题
					isImpro: '0', //本次改善是否利于减碳
					otherAdvice: '', //其它改善建议
					cartonType: '1',
					cartonInfo: '', //减碳明细
					addUser: '',
					addUserName: '',
					list: [],
				},
				factoryList: [],
				check: true, //是否利于减碳
			}
		},
		methods: {

			/**
			 * 提交
			 */
			doAdd() {
				console.log(this.fromData);
				debugger
				if (!this.fromData.factoryCode) {
					uni.showToast({
						icon: "error",
						title: "请选择线体"
					})
					return
				}
				if (!this.fromData.workProcedureCode) {
					uni.showToast({
						icon: "error",
						title: "请选择班组"
					})
					return
				}
				if (!this.fromData.improType) {
					uni.showToast({
						icon: "error",
						title: "请选择改善类别"
					})
					return
				}
				if (!this.fromData.improTitle) {
					uni.showToast({
						icon: "error",
						title: "请选择改善标题"
					})
					return
				}
				if (!this.fromData.cartonInfo) {
					uni.showToast({
						icon: "error",
						title: "请选择改善内容"
					})
					return
				}
				if (this.fromData.isImpro == '0') {
					if (this.fromData.list.length == 0) {
						uni.showToast({
							icon: "error",
							title: "请选择减碳种类"
						})
						return
					} else {
						let tag = ''
						this.fromData.list.forEach(item => {
							if (!item.reduceCount) {
								tag = item
								return
							}
						})
						if (tag) {
							uni.showToast({
								icon: "error",
								title: tag.reduceType + "减碳量为空"
							})
							return
						}
					}
				}
				this.fromData.cartonType = this.menuCode == "SCGSJT" ? "2" : "1"
				processImprovementApi.insertProcessImpro(this.fromData, (res) => {
					uni.showToast({
						icon: 'success',
						title: "提交成功"
					})
					// setTimeout(() => {
					// 	uni.navigateBack()
					// }, 1000)
				})
			},
			/**
			 * 删除分类
			 */
			deletType(i) {
				this.fromData.list.splice(i, 1)
			},
			/**
			 * 选择减碳种类
			 */
			cerTypeSelect() {
				let that = this
				
				let tag = this.menuCode == "SCGSJT"?"生产":"工艺"
				this.navigateToSelectMultiple({
					url: commonConstants.request.context.path.cer +
						'/cer/typeGroup/getTypeGroupComboByGroup/'+tag ,
					param: {},
					isPager: false,
				}, (item) => {
					console.log(item);
					let other= JSON.parse(item.otherInfo)
					this.fromData.list.push({
						reduceType: item.value,
						reduceCount: '',
						reduceUnit:other.energyUnit,
						coefficient:other.coefficient,
					})
				})
			},
			/**
			 * 选择改善类别
			 */
			improTypeSelect() {
				let that = this
				this.navigateToSelectMultiple({
					url: commonConstants.request.context.path.cer +
						'/cer/improType/getComboList',
					param: {},
					isPager: false,
				}, (item) => {
					this.fromData.improType = item.value
				})
			},
			/**
			 * 选择班组
			 */
			workProcedureSelect() {

				let that = this
				// this.navigateToSelectMultiple({
				// 	url: commonConstants.request.context.path.base +
				// 		'/basedata/organization/getOrganizationCombList',
				// 	param: {
				// 		"modelType": 50
				// 	},
				// 	isPager: false,
				// }, (item) => {
				// 	this.fromData.workProcedureName = item.name
				// 	this.fromData.workProcedureCode = item.value
				// })
				this.navigateToSelectMultiple({
					url: commonConstants.request.context.path.cer +
						'/basedata/definedDepartment/getComboList',
					param: {
						"modelType": 50,
						'parentCode':that.fromData.factoryCode
					},
					isPager: false,
				}, (item) => {
					this.fromData.workProcedureName = item.name
					this.fromData.workProcedureCode = item.value
				})
			},
			navigateToSelectMultiple(data, collback) {
				let str=''
				
				if(data.isPager !='undefined'){
					str = str+'&ip=' + data.isPager
				}
				if(data.param){
					str = str+'&pa=' +JSON.stringify(data.param)
				}
				if( data.fuzzyList &&  data.fuzzyList.length >0){
					str = str+'&fz=' + data.fuzzyList
				}
				uni.navigateTo({
					url: '/pages/index/lxsh-select-list?ul=' + data.url + str,
					events: {
						getSelect: function(data) {
							if (collback) {
								collback(data)
							}
						}
					},
				})
			},
			/**
			 * 开关选择器
			 */
			switchChange(event) {
				this.check = event.detail.value

				this.fromData.isImpro = event.detail.value ? '0' : '1'
			},
			/**
			 * 获取 工厂列表（线体）
			 */
			getFactoryComboList() {
				processImprovementApi.getFactoryComboList((res) => {
					if (res.hasData) {
						that.factoryList = res.list
					} else {
						uni.showToast({
							title: "获取配置线体为空，请检查后台配置"
						})
					}
				})
			},
			/**
			 * 选择线体
			 */
			workLinesSelect() {
				let arry = []
				this.factoryList.forEach(item => {
					arry.push(item.name)
				})
				uni.showActionSheet({
					itemList: arry,
					success: (i) => {
						this.fromData.factoryName = this.factoryList[i.tapIndex].name
						this.fromData.factoryCode = this.factoryList[i.tapIndex].value
					}
				})
			},

		},
		onLoad(opester) {
			if (opester && opester.title) {
				uni.setNavigationBarTitle({
					title: opester.title
				});
			}

			if (opester && opester.menuCode) {
				this.menuCode = opester.menuCode
			}
			that = this
			this.getFactoryComboList()
		}
	}
</script>

<style>
	@import url('../../style/item.css');
</style>